/**
 * Created by guoff on 2018-12-26.
 */

import java.io.*;
import java.util.Properties;

import static java.sql.DriverManager.println;

public class readfile
{
    //生成文件路径
    private static String path = "";
    //文件路径+名称
    private static String filenameTemp;
    //以File作为输入源File->FileReader
    public static void test1(File source) throws Exception {
        FileReader m=new FileReader(source);
        BufferedReader reader=new BufferedReader(m);
        //读取属性文件conf.properties
        Properties prop = new Properties();
        InputStream in = new BufferedInputStream (new FileInputStream(path+"/conf.properties"));
        prop.load(in);     ///加载属性列表
        System.out.println("rawKey:"+prop.getProperty("rawKey"));
        System.out.println("heiKey:"+prop.getProperty("heiKey"));
        System.out.println("datasourceKey:"+prop.getProperty("datasourceKey"));
        AES aes =new AES(prop.getProperty("rawKey"),prop.getProperty("heiKey"),prop.getProperty("datasourceKey"));
//        delFile(path+"/out.txt");
        createFile("/out","");
        int count = 0;
        String outlines="";
        while(true) {
            String nextline=reader.readLine();
            if(nextline==null) break;
            String jm = aes.getCarNo(nextline);
//            System.out.println(count+":"+jm);

            if(count==1000){
//                System.out.println("got:"+outlines);
                writeFileContent(filenameTemp,outlines);
                outlines="";
                count=0;
            }
            outlines=outlines+jm+"\n";
//            System.out.println("got:"+count);

            count ++;

        }
        writeFileContent(filenameTemp,outlines);
        outlines="";
        reader.close();

    }

    /**
     * 创建文件
     * @param fileName  文件名称
     * @param filecontent   文件内容
     * @return  是否创建成功，成功则返回true
     */
    public static boolean createFile(String fileName,String filecontent){
        Boolean bool = false;
        filenameTemp = path+fileName+".txt";//文件路径+名称+文件类型
        File file = new File(filenameTemp);
        try {
            //如果文件不存在，则创建新的文件
            if(!file.exists()){
                file.createNewFile();
                bool = true;
                System.out.println("success create file,the file is "+filenameTemp);
                //创建文件成功后，写入内容到文件里
                if(filecontent!=""){
                    writeFileContent(filenameTemp, filecontent);
                }


            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bool;
    }

    /**
     * 向文件中写入内容
     * @param filepath 文件路径与名称
     * @param newstr  写入的内容
     * @return
     * @throws IOException
     */
    public static boolean writeFileContent(String filepath,String newstr) throws IOException {
        Boolean bool = false;
        if(newstr=="" || newstr=="\n"){
            return bool;
        }
        String filein = newstr;//新写入的行，换行+"\r\n"
        String temp  = "";

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        FileOutputStream fos  = null;
        PrintWriter pw = null;
        try {
            File file = new File(filepath);//文件路径(包括文件名称)
            //将文件读入输入流
            fis = new FileInputStream(file);
            isr = new InputStreamReader(fis);
            br = new BufferedReader(isr);
            StringBuffer buffer = new StringBuffer();

            //文件原有内容
            for(int i=0;(temp =br.readLine())!=null;i++){
                buffer.append(temp);
                // 行与行之间的分隔符 相当于“\n”
                buffer = buffer.append(System.getProperty("line.separator"));
            }
            buffer.append(filein);

            fos = new FileOutputStream(file);
            pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            bool = true;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            //不要忘记关闭
            if (pw != null) {
                pw.close();
            }
            if (fos != null) {
                fos.close();
            }
            if (br != null) {
                br.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return bool;
    }

    /**
     * 删除文件
     * @param fileName 文件名称
     * @return
     */
    public static boolean delFile(String fileName){
        Boolean bool = false;
        filenameTemp = path+fileName+".txt";
        File file  = new File(filenameTemp);
        try {
            if(file.exists()){
                file.delete();
                bool = true;
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return bool;
    }

    //以InputStream作为输入源 InputStream->InputStreamReader
    public static void test2(InputStream source)throws Exception{
        InputStreamReader m=new InputStreamReader(source,"GBK");
        BufferedReader reader=new BufferedReader(m);

        while(true) {
            String nextline=reader.readLine();
            if(nextline==null) break;
//            System.out.println("got:"+nextline);
        }
        reader.close();
    }

    public static void test3()throws Exception
    {
        InputStreamReader m=new InputStreamReader(System.in);
        BufferedReader reader=new BufferedReader(m);
        while(true) {
            System.out.print(">");
            String nextline=reader.readLine();
            if(nextline==null) break;
            if("exit".equals(nextline)) {
                System.out.println("Good Bye");
                break;
            }
            //处理用户输入
//            System.out.println("handle command:"+nextline);
        }
        reader.close();
    }

    public static void main(String[] args){
        try {
            path = System.getProperty("user.dir");
            System.out.println(path);//user.dir指定了当前的路径
            delFile("/out");
//            direcotry.getPath();                    //得到的是abc
            test1(new File(path+"/in.txt"));
            //test2(new FileInputStream("c:/example/aaa"));
            //test3();
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
